package com.bdmc.device.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.device.domain.DevHealthRealtime;
import com.bdmc.device.domain.bo.DevHealthRealtimeBo;
import com.bdmc.device.domain.vo.DevHealthRealtimeVo;
import com.bdmc.device.mapper.DevHealthRealtimeMapper;
import com.bdmc.device.service.IDevHealthRealtimeService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 手环实时健康数据Service业务层处理
 *
 * @author bdmc
 * @date 2023-08-01
 */
@Service
public class DevHealthRealtimeServiceImpl extends ServicePlusImpl<DevHealthRealtimeMapper, DevHealthRealtime, DevHealthRealtimeVo> implements IDevHealthRealtimeService {

    @Override
    public DevHealthRealtimeVo queryById(Long healthId){
        return getVoById(healthId);
    }

    @Override
    public TableDataInfo<DevHealthRealtimeVo> queryPageList(DevHealthRealtimeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DevHealthRealtime> lqw = buildQueryWrapper(bo);
        Page<DevHealthRealtimeVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<DevHealthRealtimeVo> mySelectPage(DevHealthRealtimeBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<DevHealthRealtimeVo> queryList(DevHealthRealtimeBo bo) {
        LambdaQueryWrapper<DevHealthRealtime> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<DevHealthRealtime> buildQueryWrapper(DevHealthRealtimeBo bo) {
        LambdaQueryWrapper<DevHealthRealtime> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDataTime() != null, DevHealthRealtime::getDataTime, bo.getDataTime());
        lqw.eq(StringUtils.isNotBlank(bo.getImei()), DevHealthRealtime::getImei, bo.getImei());
        lqw.eq(StringUtils.isNotBlank(bo.getHeart()), DevHealthRealtime::getHeart, bo.getHeart());
        lqw.eq(StringUtils.isNotBlank(bo.getTemperature()), DevHealthRealtime::getTemperature, bo.getTemperature());
        lqw.eq(StringUtils.isNotBlank(bo.getSystolic()), DevHealthRealtime::getSystolic, bo.getSystolic());
        lqw.eq(StringUtils.isNotBlank(bo.getDiastolic()), DevHealthRealtime::getDiastolic, bo.getDiastolic());
        lqw.eq(StringUtils.isNotBlank(bo.getOx()), DevHealthRealtime::getOx, bo.getOx());
        lqw.eq(StringUtils.isNotBlank(bo.getRespiratory()), DevHealthRealtime::getRespiratory, bo.getRespiratory());
        lqw.eq(StringUtils.isNotBlank(bo.getTurnover()), DevHealthRealtime::getTurnover, bo.getTurnover());
        lqw.eq(StringUtils.isNotBlank(bo.getAuto()), DevHealthRealtime::getAuto, bo.getAuto());
        lqw.eq(StringUtils.isNotBlank(bo.getBattery()), DevHealthRealtime::getBattery, bo.getBattery());
        lqw.eq(StringUtils.isNotBlank(bo.getBloodGlu()), DevHealthRealtime::getBloodGlu, bo.getBloodGlu());
        lqw.eq(StringUtils.isNotBlank(bo.getBgState()), DevHealthRealtime::getBgState, bo.getBgState());
        lqw.eq(StringUtils.isNotBlank(bo.getOnline()), DevHealthRealtime::getOnline, bo.getOnline());
        lqw.eq(bo.getHrv() != null, DevHealthRealtime::getHrv, bo.getHrv());
        lqw.eq(bo.getBarometer() != null, DevHealthRealtime::getBarometer, bo.getBarometer());
        lqw.eq(bo.getAltitude() != null, DevHealthRealtime::getAltitude, bo.getAltitude());
        return lqw;
    }

    @Override
    public Boolean insertByBo(DevHealthRealtimeBo bo) {
        DevHealthRealtime add = BeanUtil.toBean(bo, DevHealthRealtime.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setHealthId(add.getHealthId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(DevHealthRealtimeBo bo) {
        DevHealthRealtime update = BeanUtil.toBean(bo, DevHealthRealtime.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(DevHealthRealtime entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public DevHealthRealtime getByImei(String imei) {
        LambdaQueryWrapper<DevHealthRealtime> lqw = Wrappers.lambdaQuery();
        lqw.eq(DevHealthRealtime::getImei,imei).orderByDesc(DevHealthRealtime::getHealthId).last(" limit 1");
        return getOne(lqw);
    }
}
